#include "WindowsFileSystem.h"
#include <Windows.h>


namespace Engine2D
{
    FileSystem & FileSystem::Instance()
    {
        static WindowsFileSystem instance;
        return instance;
    }

    std::string WindowsFileSystem::GetFileName(const std::string &currentPath, const std::string &patten)
    {
        char szFilePath[1024] = {0};   // 所选择的文件最终的路径
        OPENFILENAME ofn= {0};   
        ofn.lStructSize = sizeof(ofn);   
        ofn.lpstrFilter = patten.c_str();//要选择的文件后缀   
        ofn.lpstrInitialDir = currentPath.c_str();//默认的文件路径   
        ofn.lpstrFile = szFilePath;//存放文件的缓冲区   
        ofn.nMaxFile = sizeof(szFilePath)/sizeof(*szFilePath);   
        ofn.nFilterIndex = 0;   
        ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER | OFN_NOCHANGEDIR ; //标志如果是多选要加上OFN_ALLOWMULTISELECT 
        if (!GetOpenFileName(&ofn))
        {
            return "";
        }
        if (strcmp(szFilePath, "") == 0)
        {
            // 检验是否获取成功
            return "";
        }
        return szFilePath;
    }

    std::string WindowsFileSystem::SaveFileName(const std::string &currentPath, const std::string &patten)
    {
        char szFilePath[1024] = {0};   // 所选择的文件最终的路径
        OPENFILENAME ofn= {0};   
        ofn.lStructSize = sizeof(ofn);   
        ofn.lpstrFilter = patten.c_str();//要选择的文件后缀   
        ofn.lpstrInitialDir = currentPath.c_str();//默认的文件路径   
        ofn.lpstrFile = szFilePath;//存放文件的缓冲区
        ofn.nMaxFile = sizeof(szFilePath)/sizeof(*szFilePath);   
        ofn.nFilterIndex = 0;   
        ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER | OFN_NOCHANGEDIR ; //标志如果是多选要加上OFN_ALLOWMULTISELECT 
        if (!GetSaveFileName(&ofn))
        {
            return "";
        }
        if (strcmp(szFilePath, "") == 0)
        {
            // 检验是否获取成功
            return "";
        }
        return szFilePath;
    }

    bool WindowsFileSystem::CopyToClipboard(const std::string &str)
    {
    	HWND hWnd = NULL;
    	OpenClipboard( hWnd );//打开剪切板
    	EmptyClipboard();//清空剪切板
    	HANDLE hHandle = GlobalAlloc(GMEM_FIXED, 2048);//分配内存
    	char* pData = (char*) GlobalLock(hHandle);//锁定内存，返回申请内存的首地址
    	strcpy_s(pData, 2048, str.c_str());
    	SetClipboardData(CF_TEXT, hHandle);//设置剪切板数据
    	GlobalUnlock(hHandle);//解除锁定
    	CloseClipboard();//关闭剪切板
        return true;
    }
} // namespace Engine2D
